package com.hup.sb.view;

import com.alibaba.fastjson.JSON;
import com.hup.utils.commons.exception.AppCommonException;
import com.hup.utils.commons.exception.AppExceptionHandler;
import com.hup.utils.commons.exception.ErrInfo;
import com.hup.utils.commons.hardware.HRobotJni;
import com.hup.utils.commons.hardware.HRobotJni.DisplayAffinity;
import com.hup.utils.commons.start.DialogHelper;
import com.hup.utils.swing.FrameUtil;
import lombok.extern.slf4j.Slf4j;

import javax.swing.JWindow;
import java.awt.Color;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.Rectangle;
import java.util.Objects;

/**
 * 实现改变亮度的遮罩窗口
 *
 * @author hugan
 * @date 2024/6/11
 */
@Slf4j
public class ShadeWindow extends JWindow {

    private static boolean initError = false;

    /**
     * 当前[显示器bounds]
     */
    private Rectangle currentBounds;

    public ShadeWindow(GraphicsDevice device) {
        if (initError) {
            dispose();//没必要??
            return;
        }

        try {
            FrameUtil.setMousePenetration(this);

            GraphicsConfiguration[] configurations = device.getConfigurations();
            if (configurations.length < 1) throw new AppCommonException("configurations==0?");
            if (configurations.length > 1)
                log.warn("configurations={}", JSON.toJSONString(configurations));
            GraphicsConfiguration configuration = configurations[0];//只取第一个
            Rectangle bounds = configuration.getBounds();

            //设置大小: 四周留间隙,避免可能影响自动隐藏的window状态栏
            Rectangle realBounds = new Rectangle(bounds);
            realBounds.x += 1;
            realBounds.y += 1;
            realBounds.width -= 2;
            realBounds.height -= 2;
            setBounds(realBounds);
            currentBounds = bounds;
            //log.info("screen={}", realBounds);

            getContentPane().setBackground(Color.BLACK);
            setAlwaysOnTop(true);
            long hwnd = HRobotJni.getHwnd(this);
            HRobotJni.setWindowDisplayAffinity(hwnd, DisplayAffinity.WDA_EXCLUDEFROMCAPTURE);
            //不用setVisible
        } catch (Throwable e) {
            initError = true;

            dispose();
            ErrInfo errInfo = AppExceptionHandler.handle("初始化遮罩层错误:", e);
            DialogHelper.confirm(errInfo.getErrMsg())
                    .title(DialogHelper.ERROR)
                    .onTop(true)
                    .show();
        }
    }

    /**
     * 更新亮度
     *
     * @param alpha 0-全透明,最亮,1-不透明,变暗
     */
    public void updateShade(float alpha) {
        if (initError) return;

        //log.info("alpha={}", alpha);
        boolean needShow = alpha != 0;
        FrameUtil.setAlpha(this, alpha);
        boolean setAffinity = !isVisible() && needShow;
        setVisible(needShow);

        if (setAffinity) {
            //隐藏并再次显示时,要重新设定值
            try {
                long hwnd = HRobotJni.getHwnd(this);
                HRobotJni.setWindowDisplayAffinity(hwnd, DisplayAffinity.WDA_NONE);
                HRobotJni.setWindowDisplayAffinity(hwnd, DisplayAffinity.WDA_EXCLUDEFROMCAPTURE);
            } catch (ReflectiveOperationException ignored) {
            }
        }
    }

    public void updateBounds(GraphicsDevice device) {
        if (initError) return;

        GraphicsConfiguration configuration = device.getConfigurations()[0];//创建实例时,已校验过
        Rectangle bounds = configuration.getBounds();
        if (Objects.equals(bounds, currentBounds)) return;

        Rectangle realBounds = new Rectangle(bounds);
        realBounds.x += 1;
        realBounds.y += 1;
        realBounds.width -= 2;
        realBounds.height -= 2;
        setBounds(realBounds);
        currentBounds = bounds;
        //log.info("{}", bounds);
    }
}
